#include "Common.h"

#include "App_Files.h"
#include "Sock/AppLibSock_Pair.hpp"

/**********************************************************
 * Function Name:        AppLibSockPair_Send
 * Description:          写入数据
 * Parameters[in]:       IN LIB_SOCK_PAIR_s *pstSrv
 *                       IN void *data
 *                       IN SINT32 len
 * Parameters[out]:
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.25
***********************************************************/
ULONG AppLibSockPair_Send(IN APP_LIB_SOCK_PAIR_s *pstSrv, IN void *data, IN SINT32 len)
{
    return AppFile_Write(pstSrv->stSock.aiSock[pstSrv->enType], data, len);;
}

/**********************************************************
 * Function Name:        AppLibSockPair_Recv
 * Description:          读取数据
 * Parameters[in]:       IN LIB_SOCK_PAIR_s *pstSrv
 *                       IN void *data
 *                       IN SINT32 len
 * Parameters[out]:      
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.25
***********************************************************/
ULONG AppLibSockPair_Recv(IN APP_LIB_SOCK_PAIR_s *pstSrv, IN void *data, IN SINT32 len)
{
    return AppFile_Read(pstSrv->stSock.aiSock[pstSrv->enType], data, len);
}

/**********************************************************
 * Function Name:        AppLibSockPair_Creat
 * Description:          创建匿名unix socket
 * Parameters[in]:       IN LIB_SOCKET_TYPE_e enType
 *                       INOUT LIB_SOCK_s *pstSrv
 * Parameters[out]:      INOUT LIB_SOCK_s *pstSrv
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.25
***********************************************************/
ULONG AppLibSockPair_Creat(IN APP_LIB_SOCKET_TYPE_e enType, INOUT APP_LIB_SOCK_PAIR_s *pstSrv)
{
    APP_LIB_SOCK_s *pstSock = &pstSrv->stSock;

    pstSock->enType = enType;

    return AppLibSock_SocketPair(pstSock);
}

/**********************************************************
 * Function Name:        ApplibSockPair_SetRole
 * Description:          设置Socket Role
 * Parameters[in]:       IN APP_LIB_SOCK_PAIR_TYPE_e enType
 *                       INOUT APP_LIB_SOCK_PAIR_s *pstSrv
 * Parameters[out]:      INOUT APP_LIB_SOCK_PAIR_s *pstSrv
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.25
***********************************************************/
ULONG ApplibSockPair_SetRole(IN APP_LIB_SOCK_TYPE_e enType, INOUT APP_LIB_SOCK_PAIR_s *pstSrv)
{
    if (pstSrv == NULL || enType >= APP_LIB_SOCK_TYPE_MAX)
    {
        return STAT_ERR_INVAILD_PARAM;
    }

    if (enType == APP_LIB_SOCK_TYPE_SRV)
    {
        close(pstSrv->stSock.aiSock[APP_LIB_SOCK_TYPE_CLI]);
    }
    else
    {
        close(pstSrv->stSock.aiSock[APP_LIB_SOCK_TYPE_SRV]);
    }

    pstSrv->enType = enType;

    return STAT_SUCCESS;
}
